λ¬Έμ
μν¬μλ μνμ ν¬κΈ°ν μ¬λμ μ€λ§μ λλ€. μν¬μ μΌμΈλ°©μ λͺ¨μκ³ μ¬μ μν λ¬Έμ λ₯Ό μ λΆ μ°μΌλ € ν©λλ€. μν¬μλ 1λ² λ¬Έμ λΆν° λ§μ§λ§ λ¬Έμ κΉμ§ λ€μκ³Ό κ°μ΄ μ°μ΅λλ€.
1λ² μν¬μκ° μ°λ λ°©μ: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... 2λ² μν¬μκ° μ°λ λ°©μ: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ... 3λ² μν¬μκ° μ°λ λ°©μ: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...
1λ² λ¬Έμ λΆν° λ§μ§λ§ λ¬Έμ κΉμ§μ μ λ΅μ΄ μμλλ‘ λ€μ λ°°μ΄ answersκ° μ£Όμ΄μ‘μ λ, κ°μ₯ λ§μ λ¬Έμ λ₯Ό λ§ν μ¬λμ΄ λꡬμΈμ§ λ°°μ΄μ λ΄μ return νλλ‘ solution ν¨μλ₯Ό μμ±ν΄μ£ΌμΈμ.
μ νμ¬ν
- μνμ μ΅λ 10,000 λ¬Έμ λ‘ κ΅¬μ±λμ΄μμ΅λλ€.
- λ¬Έμ μ μ λ΅μ 1, 2, 3, 4, 5μ€ νλμ λλ€.
- κ°μ₯ λμ μ μλ₯Ό λ°μ μ¬λμ΄ μ¬λΏμΌ κ²½μ°, returnνλ κ°μ μ€λ¦μ°¨μ μ λ ¬ν΄μ£ΌμΈμ.
μ μΆλ ₯ μ
| answers | return |
|---|---|
| [1,2,3,4,5] | [1] |
| [1,3,2,4,2] | [1,2,3] |
π Logic
private int countScore(int[] answers, int[] std) {
int score = 0;
for (int i = 0, idx = 0; i < answers.length; i++) {
if (idx >= std.length)
idx = idx % std.length;
if (std[idx++] == answers[i])
score++;
}
return score;
}
- μ£Όμ΄μ§ νμμ μ λ΅ λ°°μ΄μ κ°μ§κ³ μ μλ₯Ό λ§€κΈ΄λ€.
- λ€λ§ μ£Όμ΄μ§ μ λ΅ λ°°μ΄μ λ²μλ₯Ό λ²μ΄λλ κ²½μ° (λ¬Έμ μκ° λ λ§μ κ²½μ°) μ κ·Ό index κ°μ
%μ μ¬μ©ν΄ λ³κ²½νλ€.
π CODE
μ½λ 보기/μ κΈ°π«
import java.util.*;
class Solution {
public int[] solution(int[] answers) {
int[] std1 = {1,2,3,4,5};
int[] std2 = {2,1,2,3,2,4,2,5};
int[] std3 = {3,3,1,1,2,2,4,4,5,5};
int[] scores = new int[3];
scores[0] = countScore(answers, std1);
scores[1] = countScore(answers, std2);
scores[2] = countScore(answers, std3);
int max = Arrays.stream(scores).max().getAsInt();
ArrayList<Integer> answer = new ArrayList<>();
for (int i = 0; i < 3; i++) {
if (scores[i] == max)
answer.add(i + 1);
}
return answer.stream().mapToInt(i -> i).toArray();
}
private int countScore(int[] answers, int[] std) {
int score = 0;
for (int i = 0, idx = 0; i < answers.length; i++) {
if (idx >= std.length) idx = idx % std.length;
if (std[idx++] == answers[i])
score++;
}
return score;
}
}
βοΈ Review
- μμ νμ μκ³ λ¦¬μ¦λ΅κ² λ¨μν λ€ λλ €λ³΄λ©΄ λλ λ¬Έμ μλ€.
π μΆμ²
Programmers : https://programmers.co.kr/learn/courses/30/lessons/42840